package za.org.jcicada.genss.sensor.impl;

import za.org.jcicada.genss.sensor.ISensorType;

/**
 * Integer sensor type with legal range.
 * @author rcrida
 */
public class TypeInt implements ISensorType {

	private static final long serialVersionUID = 1L;
	private int min;
    private int max;
    
    /**
     * Factory method used to access instance of TypeInt
     * @return an instance of TypeFloat
     */
    static public TypeInt getInstance(int min, int max) {
        return new TypeInt(min, max);
    }
    
    /**
     * Factory method used to access instance of TypeFloat
     * @param args string array with min and max values
     * @return an instance of TypeFloat
     */
    static public TypeInt getInstance(String[] args) {
        return getInstance(Integer.parseInt(args[0]), Integer.parseInt(args[1]));
    }
    
    /**
     * Private constructor forces use of getInstance().
     * @param min the minimum legal integer value
     * @param max the maximum legal integer value
     */
    private TypeInt(int min, int max) {
        this.min = min;
        this.max = max;
    }
    
    public boolean isValid(Object value) {
        if (!(value instanceof Integer)) {
            return false;
        }
        int val = ((Integer)value).intValue();
        return val >= min && val <= max;
    }
    
    @Override
    public String toString() {
    	return "Int";
    }

    @Override
    public boolean equals(Object aOther) {
        if (this == aOther) return true;
        if (!(aOther instanceof TypeInt)) return false;
        TypeInt other = (TypeInt)aOther;
        return min == other.min && max == other.max;
    }
    
    @Override
    public int hashCode() {
        return min + max;
    }

    public String[] toStringArray() {
        return new String[]{Integer.toString(min), Integer.toString(max)};
    }

    public Object valueOf(String string) {
        return Integer.valueOf(string);
    }
    
    public double doubleValueOf(Object value) {
    	return ((Number) value).doubleValue();
    }
    
    public int getMin() {
    	return min;
    }
    
    public int getMax() {
    	return max;
    }

}
